#!/bin/sh
#
# Copyright (C) 2000-2025 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Test running a Virtual disk changer with two Media Types
#   and two different Archive directories
#   This tests bug #8033 where a Volume is not truncated,
#   but a second Volume of the same name is made in the wrong
#   directory.
#
TestName="2media-virtual-test"
JobName=Virtual
. scripts/functions

${rscripts}/cleanup
cp -f ${rscripts}/bacula-dir-2media-virtual.conf ${conf}/bacula-dir.conf
cp -f ${rscripts}/bacula-sd-2media-virtual.conf ${conf}/bacula-sd.conf
cp -f ${rscripts}/test-bacula-fd.conf ${conf}/bacula-fd.conf
cp -f ${rscripts}/test-console.conf ${conf}/bconsole.conf
rm -rf ${tmp}/disk ${tmp}/disk1
mkdir -p ${tmp}/disk ${tmp}/disk1

disable_plugins
echo "$cwd/build/src/dird" >${cwd}/tmp/file-list

start_test

cat <<END_OF_DATA >${tmp}/bconcmds
@output /dev/null
messages
@$out ${tmp}/log1.out
@#setdebug level=15 storage=Virtual
@#setdebug level=200 client
@#setdebug level=100 director
label storage=Virtual volume=TestVolume001 pool=Default drive=0 slot=0
label Storage=vDrive-3 volume=TestVolume002 pool=Default drive=2 slot=0
run job=$JobName level=Full storage=Virtual yes
wait
update volume=TestVolume001 volstatus=Used actiononpurge=truncate
run job=$JobName level=Full storage=vDrive-3 yes
wait
messages
list volumes
llist volume=TestVolume001
purge volume=TestVolume001
update volume=TestVolume002 volstatus=Used actiononpurge=truncate
purge volume=TestVolume002
purge volume action=truncate storage=Virtual drive=0 pool=Default
purge volume action=truncate storage=Virtual drive=2 pool=Default
list volumes
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=Virtual
stop_bacula

# No TestVolume002 should be in ${tmp}/disk
ls -l ${tmp}/disk | grep -q TestVolume002
if [ $? = 0 ]; then
   print_debug "ERROR: Volume TestVolume2 incorrectly created in ${tmp}/disk"
   dstat=1
fi

if [ x$REGRESS_DEBUG != x ] ; then
   ls -l ${tmp}/disk
   ls -l ${tmp}/disk1
fi

exit

#check_two_logs
#check_restore_diff
end_test
